package com.esri.android.map;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.esri.core.geometry.Point;
import com.esri.core.internal.tasks.TaskListener;
import com.esri.core.io.EsriServiceException;
import com.esri.core.map.CallbackListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;

/* loaded from: classes.dex */
public abstract class TiledServiceLayer extends TiledLayer {
    public static final int WEB_MERCATOR_SPATIAL_REFERENCE_WKID = 102100;
    public static final int initWithDpi = 96;
    TileInfo b;
    private ImageFilter c;
    protected boolean isBingMap;
    protected final ConcurrentHashMap<String, Future<?>> pending;

    protected abstract byte[] getTile(int i, int i2, int i3) throws Exception;

    native long nativeCreateLayer();

    native boolean nativeInitialize(long j, int i, String str, double d, double d2, double d3, double d4, double d5, double d6, double[] dArr, double[] dArr2, int i2, int i3, int i4, int i5);

    native void nativeSetCachePath(long j, String str);

    native void nativeSetTile(long j, int i, int i2, int i3, byte[] bArr, int i4);

    /* loaded from: classes.dex */
    public static class TileInfo {
        Point a;
        double[] b;
        double[] c;
        int d;
        int e;
        int f;
        int g;

        public TileInfo(Point point, double[] dArr, double[] dArr2, int i, int i2, int i3, int i4) {
            this.a = point;
            this.b = dArr;
            this.c = dArr2;
            this.d = i;
            this.e = i2;
            this.f = i3;
            this.g = i4;
        }

        public Point getOrigin() {
            return this.a;
        }

        public double[] getScales() {
            return this.b;
        }

        public double[] getResolutions() {
            return this.c;
        }

        public int getDPI() {
            return this.e;
        }

        public int getLevels() {
            return this.d;
        }

        public int getTileWidth() {
            return this.f;
        }

        public int getTileHeight() {
            return this.g;
        }
    }

    public TiledServiceLayer(String str) {
        super(str, true);
        this.pending = new ConcurrentHashMap<>();
        this.isBingMap = false;
    }

    public TiledServiceLayer(boolean z) {
        super(z);
        this.pending = new ConcurrentHashMap<>();
        this.isBingMap = false;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.Layer
    public long create() {
        return nativeCreateLayer();
    }

    public TileInfo getTileInfo() {
        return this.b;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public void setTileInfo(TileInfo tileInfo) {
        this.b = tileInfo;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void b(String str) {
        if (this.nativeHandle != 0) {
            nativeSetCachePath(this.nativeHandle, str);
        }
    }

    /* loaded from: classes.dex */
    class b<T> implements CallbackListener<T> {
        private final int b;
        private final int c;
        private final int d;
        private int e;

        public b(int i, int i2, int i3, int i4) {
            this.e = i;
            this.b = i2;
            this.d = i4;
            this.c = i3;
        }

        @Override // com.esri.core.map.CallbackListener
        public void onError(Throwable th) {
            if ((th instanceof InterruptedException) || (th instanceof InterruptedIOException)) {
                TiledServiceLayer.this.a(this.b, this.c, this.d, new byte[0]);
            } else if (th instanceof EsriServiceException) {
                TiledServiceLayer.this.a(this.b, this.c, this.d, new byte[0]);
                Log.w("ArcGIS.TileCache", "EsriServiceException:" + this.b + "," + this.c + "," + this.d + ":" + th.getMessage() + " Response Code=:" + ((EsriServiceException) th).getCode());
            } else if (th instanceof OutOfMemoryError) {
                Log.e("ArcGIS.TileCache", "XXX out of memory:" + this.b + "," + this.c + "," + this.d, th);
                a();
            } else {
                TiledServiceLayer.this.a(this.b, this.c, this.d, new byte[0]);
                Log.e("ArcGIS.TileCache", "Exception occur:" + this.b + "," + this.c + "," + this.d, th);
            }
        }

        private void a() {
            if (Thread.currentThread().isInterrupted()) {
                return;
            }
            TiledServiceLayer.this.requestTile(this.b, this.c, this.d);
        }

        @Override // com.esri.core.map.CallbackListener
        public void onCallback(T t) {
            if (Thread.currentThread().isInterrupted()) {
                return;
            }
            try {
                byte[] bArr = (byte[]) t;
                if (TiledServiceLayer.this.c != null) {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    options.inMutable = true;
                    Bitmap applyFilter = TiledServiceLayer.this.c.applyFilter(BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options));
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    applyFilter.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    bArr = byteArrayOutputStream.toByteArray();
                    try {
                        byteArrayOutputStream.close();
                    } catch (IOException unused) {
                    }
                }
                TiledServiceLayer.this.a(this.b, this.c, this.d, bArr);
            } catch (OutOfMemoryError unused2) {
                a();
            }
        }
    }

    /* loaded from: classes.dex */
    class a<V> implements TaskListener<V> {
        private final String b;
        private final CallbackListener<V> c;

        public a(String str, CallbackListener<V> callbackListener) {
            this.b = str;
            this.c = callbackListener;
        }

        @Override // com.esri.core.internal.tasks.TaskListener
        public void onError(Throwable th) {
            TiledServiceLayer.this.pending.remove(this.b);
            this.c.onError(th);
        }

        @Override // com.esri.core.internal.tasks.TaskListener
        public void onCompletion(short s, V v) {
            TiledServiceLayer.this.pending.remove(this.b);
            if (this.b.startsWith(String.valueOf(TiledServiceLayer.this.getUrlHashCode()))) {
                if (s == 1) {
                    this.c.onCallback(v);
                } else {
                    this.c.onError(null);
                }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    /* JADX WARN: Removed duplicated region for block: B:13:0x0073  */
    /* JADX WARN: Removed duplicated region for block: B:14:0x007b  */
    @Override // com.esri.android.map.Layer
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public void initLayer() {
        /*
            r24 = this;
            r4 = r24
            com.esri.core.geometry.SpatialReference r0 = r24.getDefaultSpatialReference()
            if (r0 == 0) goto L70
            int r3 = r0.getID()
            java.lang.String r23 = r0.getText()
            com.esri.core.geometry.Envelope r0 = r24.getFullExtent()
            if (r0 == 0) goto L70
            boolean r1 = r0.isEmpty()
            if (r1 != 0) goto L70
            long r1 = r24.getID()
            com.esri.android.map.TiledServiceLayer$TileInfo r5 = r4.b
            com.esri.core.geometry.Point r5 = r5.a
            double r5 = r5.getX()
            com.esri.android.map.TiledServiceLayer$TileInfo r7 = r4.b
            com.esri.core.geometry.Point r7 = r7.a
            double r7 = r7.getY()
            double r9 = r0.getXMin()
            double r11 = r0.getYMin()
            double r13 = r0.getXMax()
            double r15 = r0.getYMax()
            com.esri.android.map.TiledServiceLayer$TileInfo r0 = r4.b
            double[] r17 = r0.getScales()
            com.esri.android.map.TiledServiceLayer$TileInfo r0 = r4.b
            double[] r18 = r0.getResolutions()
            com.esri.android.map.TiledServiceLayer$TileInfo r0 = r4.b
            int r19 = r0.getLevels()
            com.esri.android.map.TiledServiceLayer$TileInfo r0 = r4.b
            int r20 = r0.getDPI()
            com.esri.android.map.TiledServiceLayer$TileInfo r0 = r4.b
            int r21 = r0.getTileWidth()
            com.esri.android.map.TiledServiceLayer$TileInfo r0 = r4.b
            int r22 = r0.getTileHeight()
            r0 = r24
            r4 = r23
            boolean r0 = r0.nativeInitialize(r1, r3, r4, r5, r7, r9, r11, r13, r15, r17, r18, r19, r20, r21, r22)
            if (r0 == 0) goto L70
            r0 = 1
            goto L71
        L70:
            r0 = 0
        L71:
            if (r0 == 0) goto L7b
            com.esri.android.map.event.OnStatusChangedListener$STATUS r0 = com.esri.android.map.event.OnStatusChangedListener.STATUS.INITIALIZED
            r1 = r24
            r1.changeStatus(r0)
            goto L94
        L7b:
            r1 = r24
            boolean r0 = r1.isBingMap
            if (r0 == 0) goto L8b
            r0 = -1005(0xfffffffffffffc13, float:NaN)
            com.esri.android.map.event.OnStatusChangedListener$STATUS r0 = com.esri.android.map.event.OnStatusChangedListener.STATUS.fromInt(r0)
            r1.changeStatus(r0)
            goto L94
        L8b:
            r0 = -1001(0xfffffffffffffc17, float:NaN)
            com.esri.android.map.event.OnStatusChangedListener$STATUS r0 = com.esri.android.map.event.OnStatusChangedListener.STATUS.fromInt(r0)
            r1.changeStatus(r0)
        L94:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.esri.android.map.TiledServiceLayer.initLayer():void");
    }

    protected void requestTile(final int i, final int i2, final int i3) {
        final String a2 = a(i, i2, i3);
        if (this.pending.containsKey(a2)) {
            return;
        }
        try {
            this.pending.put(a2, getPoolExecutor().submit(new Runnable() { // from class: com.esri.android.map.TiledServiceLayer.1
                @Override // java.lang.Runnable
                public void run() {
                    TiledServiceLayer tiledServiceLayer = TiledServiceLayer.this;
                    a aVar = new a(a2, new b(tiledServiceLayer.getUrlHashCode(), i, i2, i3));
                    try {
                        aVar.onCompletion((short) 1, TiledServiceLayer.this.getTile(i, i2, i3));
                    } catch (Throwable th) {
                        aVar.onError(th);
                    }
                }
            }));
        } catch (RejectedExecutionException unused) {
        }
    }

    protected void cancelRequest(int i, int i2, int i3) {
        String a2 = a(i, i2, i3);
        Future<?> future = this.pending.get(a2);
        if (future != null) {
            try {
                future.cancel(true);
            } catch (NullPointerException unused) {
            }
        }
        a(i, i2, i3, new byte[0]);
        this.pending.remove(a2);
    }

    @Override // com.esri.android.map.Layer
    public void recycle() {
        cancelPendingTasks();
        super.recycle();
    }

    protected void cancelPendingTasks() {
        if (this.pending.isEmpty()) {
            return;
        }
        for (Map.Entry<String, Future<?>> entry : this.pending.entrySet()) {
            Future<?> value = entry.getValue();
            if (value != null) {
                try {
                    value.cancel(true);
                } catch (Exception unused) {
                }
            }
        }
        this.pending.clear();
        Log.i(com.esri.core.internal.a.c, "cancelPendingTasks...");
    }

    void a(int i, int i2, int i3, byte[] bArr) {
        if (this.nativeHandle != 0) {
            nativeSetTile(getID(), i, i2, i3, bArr, bArr != null ? bArr.length : 0);
        }
    }

    String a(int i, int i2, int i3) {
        return (getUrlHashCode() + "") + "," + i + "." + i2 + "." + i3;
    }

    public void setTileImageFilter(ImageFilter imageFilter) {
        this.c = imageFilter;
    }
}
